package com.itboye.textileexpres.myset;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.textileexpes.adapter.CityAndprovinceListAdapter;
import com.itboye.textileexpes.beans.CityBeans;
import com.itboye.textileexpes.beans.KefuBeans.CounBeans;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.util.SPContants;
import com.itboye.textileexpres.util.SPUtils;
import com.itboye.textileexpres.widget.MyListView;
import com.itboye.textillexepres.db.DBManager;

/**
 * 城市列表
 * 
 * @author sy
 * 
 */
public class ActivityChainaCity extends BaseActivity implements OnClickListener {
	private BaseAdapter adapter;
	private ListView country_lv_list;
	private Handler handler;
	private SQLiteDatabase database;
	private ArrayList<CityBeans> mCityNames;
	private ImageView back;
	EditText edit_inputcity;
	private ArrayList<CityBeans> arrayLitArea;// 区域列表
	private DBManager dbManager;
	public String city;
	int type = 1;// 1、直辖市 2、城市
	boolean searchType = true;// true、根据 城市搜索店铺 false、根据地区搜索店铺
	RelativeLayout relypoutGps;
	private TextView tvchina;
	String countryId;
	// String title;

	String provinceID;
	String country;
	String cityName;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_city;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		country_lv_list = (ListView) findViewById(R.id.country_lv_list);
		provinceID = getIntent().getStringExtra("id");
		cityName= getIntent().getStringExtra("cityName");
		dbManager = new DBManager(this);
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>provinceID" + provinceID);
		dbManager.openDateBase();
		// cityNoDBManager.openDateBase();
		database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/"
				+ DBManager.DB_NAME, null);

		// database.close();

		handler = new Handler();
		try {
			if (provinceID == null || provinceID == "") {
				finish();
			} else
				mCityNames = getCityNames();
		} catch (Exception e) {
			// TODO: handle exception
		}

		setAdapter(mCityNames);
		country_lv_list.setOnItemClickListener(new CityListOnItemClick());
	}

	/**
	 * 从数据库获取城市数据
	 * 
	 * @return
	 */
	private ArrayList<CityBeans> getCityNames() {
		ArrayList<CityBeans> names = new ArrayList<CityBeans>();
		Cursor cursor = database.rawQuery(
				"SELECT * FROM common_city where father=" + provinceID
						+ " ORDER BY city", null);
		System.out.println("cursor" + cursor);
		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.moveToPosition(i);
			CityBeans cityModel = new CityBeans();
			cityModel.setCountry_name(cursor.getString(cursor
					.getColumnIndex("city")));

			cityModel.setCountryCode(cursor.getString(cursor
					.getColumnIndex("cityID")));
			names.add(cityModel);

		}
		return names;
	}

	/**
	 * 城市列表点击事件
	 * 
	 * @author sy
	 * 
	 */
	class CityListOnItemClick implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
				long arg3) {
			
			searchType = true;
			CityBeans cityModel = (CityBeans) country_lv_list.getAdapter()
					.getItem(pos);
			city = cityModel.getCountry_name();
			cityModel.getCountryCode();
			// afterCitySelected(city);
			// 发送广播通知更改头像

			Intent intent2 = new Intent("AddressBeans");
			sendBroadcast(intent2);

			SPUtils.put(ActivityChainaCity.this, null, SPContants.COUNTRYNMAW,
					country+city);
			
			SPUtils.put(ActivityChainaCity.this, null, SPContants.AREA,
					city);
			

			SPUtils.put(ActivityChainaCity.this, null, SPContants.PROVINCE,
					cityName);
			SPUtils.put(ActivityChainaCity.this, null, SPContants.PROVINCECITYID,
					cityModel.getId());
			
			finish();
			country_lv_list.setSelection(-1);// 设置选择项为第一项
		}

	}

	/**
	 * 为ListView设置适配器
	 * 
	 * @param list
	 */
	private void setAdapter(List<CityBeans> list) {

		if (list != null) {
			adapter = new CityAndprovinceListAdapter(this, list);
			country_lv_list.setAdapter(adapter);
		} else {
			finish();
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			this.finish();
			break;
		default:
			break;
		}
	}

}